<template>
  <div class="login-container">
    <van-nav-bar title="登录" fixed />

    <van-form @submit="login">
      <van-field v-model="form.mobile" type='tel' label='手机号码' placeholder="请输入手机号码" required :rules='rules.mobile' />
      <van-field v-model="form.code" type='password' label='登录密码' placeholder="请输入登录密码" required :rules='rules.code' />

      <div style="margin:16px;">
        <van-button round block type='info' native-type='submit'>提交</van-button>
      </div>
    </van-form>

  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import { loginAPI } from '@/api/userAPI'

export default {
  name: 'Login',
  data() {
    return {
      form: {
        mobile: '13888888123',
        code: '246810'
      },
      rules: {
        mobile: [
          { required: true, message: '请填写您的手机号', trigger: 'onBlur' },
          { pattern: /^1\d{10}$/, message: '请填写您的手机号', trigger: 'onBlur' }
        ],
        code: [{ required: true, message: '请填写您的手机号密码', trigger: 'onBlur' }]

      }
    }
  },
  methods: {
    ...mapMutations(['updataTokenInfo']),
    async login() {
      const { data: res } = await loginAPI(this.form)

      if (res.message === 'OK') {
        console.log(res)
        this.updataTokenInfo(res.data)
        // 2. 要跳转到首页
        const navPath = this.$route.query.pre || '/'
        this.$router.replace(navPath)
      }
    }
  }
}
</script>

<style lang='less' scoped>
.login-container {
  padding-top: 46px;
}
</style>
